package fhp.hlhj.giantfold.activity.malls

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.widget.LinearLayout
import com.alibaba.baichuan.android.trade.AlibcTrade
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback
import com.alibaba.baichuan.android.trade.model.AlibcShowParams
import com.alibaba.baichuan.android.trade.model.OpenType
import com.alibaba.baichuan.android.trade.page.AlibcPage
import com.alibaba.baichuan.trade.biz.context.AlibcTradeResult
import com.alibaba.baichuan.trade.biz.core.taoke.AlibcTaokeParams
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.adapter.SuperMarketRcyAdp
import fhp.hlhj.giantfold.interfaces.IGoodsFgm
import fhp.hlhj.giantfold.javaBean.ErroBean
import fhp.hlhj.giantfold.javaBean.SuperMarketListBean
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.presenter.SuperMarketListPresenter
import fhp.hlhj.giantfold.utils.LogUtil
import kotlinx.android.synthetic.main.activity_search_result_aty.*
import java.util.HashMap

class SearchResultAty : BaseActivity(), IGoodsFgm {

    private lateinit var listViewAdp: SuperMarketRcyAdp
    private var datas=ArrayList<SuperMarketListBean.DataBean>()

    private lateinit var goodsPresenter: SuperMarketListPresenter
    override fun getContentId(): Int {
        return R.layout.activity_search_result_aty
    }

    override fun initView() {
        tv.text=intent.getStringExtra("so")
        goodsPresenter= SuperMarketListPresenter(this)
        listViewAdp= SuperMarketRcyAdp(R.layout.supermarket_listitem,datas,this)
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        recyclerview.adapter=listViewAdp

        val params = HttpParams()
        params.put("appkey", Contents.TKAPPKEY)
        params.put("type","so")
        params.put("keyword",intent.getStringExtra("so"))
        goodsPresenter.getGoods(params)
    }

    override fun showLoading() {
        loadingDialog.show()
    }

    override fun onError(erro: ErroBean) {
    }

    override fun getGoods(data: SuperMarketListBean) {
        loadingDialog.dismiss()
        datas.addAll(data.data)
        listViewAdp.notifyDataSetChanged()
    }

    override fun changeLayout() {
    }

    override fun setLoadmorde() {
    }

    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }
        listViewAdp.setOnItemClickListener { adapter, view, position ->
            /*打开领券详情*/
            val quanStr = datas[position].quan_link
            var quanStrs = quanStr.split("activity_id=")
            if (quanStrs.size==1)quanStrs=quanStr.split("activityId=")
            val activityId= quanStrs[1]
            val page = AlibcPage(Urls.TK_QUAN_URL+"activityId=$activityId&itemId=${datas[position].goods_id}&pid=mm_116147303_36832537_132678582")
            LogUtil.log(Urls.TK_QUAN_URL+"activityId=$activityId&itemId=${datas[position].goods_id}")
            val showParams = AlibcShowParams(OpenType.H5, false)
            val params = AlibcTaokeParams()
            params.pid="mm_116147303_36832537_132678582"
//            params.adzoneid="132678582"
            params.subPid="mm_116147303_36832537_132678582"
            params.extraParams = HashMap<String, String>()
            params.extraParams.put("taokeAppkey","24624369")
            LogUtil.log("成功设置的pid为，${params.pid}")
            AlibcTrade.show(this,page,showParams,params,null,object : AlibcTradeCallback {
                override fun onFailure(p0: Int, p1: String?) {
                    LogUtil.log("错误$p1")
                }

                override fun onTradeSuccess(p0: AlibcTradeResult) {
                    LogUtil.log("成功${p0.payResult.paySuccessOrders[0]}设置的pid为，${params.pid}")
                    if (null!=p0.payResult.paySuccessOrders&&p0.payResult.paySuccessOrders.size>0) {
                        val intent = Intent(this@SearchResultAty, TBOrderListAty::class.java)
                        intent.putExtra("pid",datas[position].goods_id)
                        intent.putExtra("et",5)
                        intent.putExtra("rate",datas[position].rate)
                        intent.putExtra("imgurl",datas[position].pic)
                        startActivity(intent)
                    }

                }
            })
            /*打开商品详情*/
            /*val showParams = AlibcShowParams(OpenType.Native, false)
            val params = AlibcTaokeParams()
            params.pid="mm_116147303_36832537_132678582"
//            params.adzoneid="132678582"
            params.subPid="mm_116147303_36832537_132678582"
            val detailPage = AlibcDetailPage(datas[position].goods_id)
            LogUtil.log("成功设置的pid为，${params.pid}")
            AlibcTrade.show(activity,detailPage,showParams,params,null,object :AlibcTradeCallback{
                override fun onFailure(p0: Int, p1: String?) {
                    LogUtil.log("失败$p1")
                }

                override fun onTradeSuccess(p0: AlibcTradeResult) {
                    LogUtil.log("成功${p0.payResult.paySuccessOrders[0]}设置的pid为，${params.pid}")
                    if (null!=p0.payResult.paySuccessOrders&&p0.payResult.paySuccessOrders.size>0){
//                        商品详情
                        val intent = Intent(context, TBOrderListAty::class.java)
                        intent.putExtra("pid",datas[position].goods_id)
                        intent.putExtra("et",5)
                        intent.putExtra("rate",datas[position].rate)
                        intent.putExtra("imgurl",datas[position].pic)
                        startActivity(intent)
                    }

                }
            })*/
        }
    }
}
